﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.teachers
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        subject sub = new subject();
        report rep = new report();
        Classes cl = new Classes();
        teacher teach = new teacher();
        remark rem = new remark();
        student stud = new student();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnupdate.Visible = false;
                Panel5.Visible = false; Panel3.Visible = false;
                DataTable dt1 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
                DataTable dt2 = rep.GetRecords(-99, "", -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
                if (dt1.Rows.Count > 0)
                {
                    Panel1.Visible = true; Panel2.Visible = false; Panel3.Visible = true;
                }
                else
                {
                    Panel2.Visible = true; Panel1.Visible = false;
                }
                //Label1.Text = "Class Name:- "+dt2.Rows[0]["class_name"].ToString();
                DataTable dt = sub.GetRecords(-99, "", -99, -99, false, " and ");
                ddsubject.DataSource = dt;
                ddsubject.DataTextField = "subject_name";
                ddsubject.DataValueField = "subject_id";
                ddsubject.DataBind();
                ddsubject.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-Select-", "-99"));
                ddsubject0.DataSource = dt;
                ddsubject0.DataTextField = "subject_name";
                ddsubject0.DataValueField = "subject_id";
                ddsubject0.DataBind();
                ddsubject0.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-Select-", "-99"));
                ddlsubjectid.DataSource = dt;
                ddlsubjectid.DataTextField = "subject_name";
                ddlsubjectid.DataValueField = "subject_id";
                ddlsubjectid.DataBind();
                ddlsubjectid.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-Select-", "-99"));
                //Label2.Text = "Subject Name:- " + dt.Rows[0]["subject_name"].ToString();
                //ddclass.DataSource = dt1;
                //ddclass.DataTextField = "class_name";
                //ddclass.DataValueField = "class_id";
                //ddclass.DataBind();


                DataTable dt3 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
                ddlclassid.DataSource = dt3;
                ddlclassid.DataTextField = "class_name";
                ddlclassid.DataValueField = "class_id";
                ddlclassid.DataBind();
                ddlclassid.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-Select-", "-99"));
                ddlstudentid.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-Select-", "-99"));

                DataTable dt5 = rem.GetRecords(-99, "", false, " and ");
                ddlremarkid.DataSource = dt5;
                ddlremarkid.DataTextField = "remark_name";
                ddlremarkid.DataValueField = "remark_id";
                ddlremarkid.DataBind();


            }
        }

        protected void btnview_Click(object sender, EventArgs e)
        {
            DataTable dt1 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
            DataTable dt = rep.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, Convert.ToInt64(ddsubject.SelectedValue.ToString()), -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void btnview2_Click(object sender, EventArgs e)
        {
            DataTable dt = rep.GetRecords(-99, "", -99, -99, Convert.ToInt64(ddsubject0.SelectedValue.ToString()), -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }

        protected void ddclass_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddsubject0_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            Panel3.Visible = true; btnupdate.Visible = true;
            btninsert.Visible = false;
            if (e.CommandName == "Edit")
            {
                DataTable dt = rep.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
                tbreportid.Text = dt.Rows[0]["report_id"].ToString();
                ddexam.SelectedValue = dt.Rows[0]["examination"].ToString();
                ddlclassid.SelectedValue = dt.Rows[0]["class_id"].ToString();
                ddlclassid_SelectedIndexChanged(null, null);
                ddlstudentid.SelectedValue = dt.Rows[0]["student_id"].ToString();
                tbrollno.Text = dt.Rows[0]["roll_no"].ToString();
                ddlsubjectid.SelectedValue = dt.Rows[0]["subject_id"].ToString();
                tbtheorymax.Text = dt.Rows[0]["max_theorymarks"].ToString();
                tbpracmax.Text = dt.Rows[0]["max_practicalmarks"].ToString();
                tbpromax.Text = dt.Rows[0]["max_projectmarks"].ToString();
                tbtheoryobt.Text = dt.Rows[0]["obt_theorymarks"].ToString();
                tbpracobt.Text = dt.Rows[0]["obt_practicalmarks"].ToString();
                tbproobt.Text = dt.Rows[0]["obt_projectmarks"].ToString();
                tbtotmarks.Text = dt.Rows[0]["total_maxmarks"].ToString();
                tbtobtmarks.Text = dt.Rows[0]["total_obtmarks"].ToString();
                tbper.Text = dt.Rows[0]["percentage"].ToString();
                tbgrade.Text = dt.Rows[0]["grade"].ToString();
                ddlremarkid.SelectedValue = dt.Rows[0]["remark_id"].ToString();
            }
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName=="Delete")
            {
                DataTable dt = rep.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
                rep.report_id = Convert.ToInt64(dt.Rows[0]["report_id"].ToString());
                rep.examination= dt.Rows[0]["examination"].ToString();
                rep.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                rep.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                rep.subject_id = Convert.ToInt64(dt.Rows[0]["subject_id"].ToString());
                rep.max_theorymarks= Convert.ToInt32(dt.Rows[0]["max_theorymarks"].ToString());
                rep.max_practicalmarks = Convert.ToInt32(dt.Rows[0]["max_practicalmarks"].ToString());
                rep.max_projectmarks = Convert.ToInt32(dt.Rows[0]["max_projectmarks"].ToString());
                rep.obt_theorymarks = Convert.ToInt32(dt.Rows[0]["obt_theorymarks"].ToString());
                rep.obt_practicalmarks = Convert.ToInt32(dt.Rows[0]["obt_practicalmarks"].ToString());
                rep.obt_projectmarks = Convert.ToInt32(dt.Rows[0]["obt_projectmarks"].ToString());
                rep.total_maxmarks = Convert.ToInt32(dt.Rows[0]["total_maxmarks"].ToString());
                rep.total_obtmarks = Convert.ToInt32(dt.Rows[0]["total_obtmarks"].ToString());
                rep.percentage= Convert.ToInt64(dt.Rows[0]["percentage"].ToString());
                rep.grade= dt.Rows[0]["grade"].ToString();
                rep.remark_id = Convert.ToInt64(dt.Rows[0]["remark_id"].ToString());
                rep.isdeleted = true;
                rep.SaveRecords();
                Response.Redirect("~/teachers/TReport.aspx");

            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            rep.report_id = -99;
            rep.examination = ddexam.SelectedValue.ToString();
            rep.class_id = Convert.ToInt64(ddlclassid.SelectedValue.ToString());
            rep.student_id = Convert.ToInt64(ddlstudentid.SelectedValue.ToString());
            rep.subject_id = Convert.ToInt64(ddlsubjectid.SelectedValue.ToString());
            rep.max_theorymarks = Convert.ToInt16(tbtheorymax.Text);
            rep.max_practicalmarks = Convert.ToInt16(tbpracmax.Text);
            rep.max_projectmarks = Convert.ToInt16(tbpromax.Text);
            rep.obt_theorymarks = Convert.ToInt16(tbtheoryobt.Text);
            rep.obt_practicalmarks = Convert.ToInt16(tbpracobt.Text);
            rep.obt_projectmarks = Convert.ToInt16(tbproobt.Text);
            int a1 = Convert.ToInt16(tbtheorymax.Text);
            int a2 = Convert.ToInt16(tbpracmax.Text);
            int a3 = Convert.ToInt16(tbpromax.Text);
            int s1 = a1 + a2 + a3;
            tbtotmarks.Text = s1.ToString();
            int b1 = Convert.ToInt16(tbtheoryobt.Text);
            int b2 = Convert.ToInt16(tbpracobt.Text);
            int b3 = Convert.ToInt16(tbproobt.Text);
            int s2 = b1 + b2 + b3;
            tbtobtmarks.Text = s2.ToString();
            rep.total_maxmarks = Convert.ToInt16(tbtotmarks.Text);
            rep.total_obtmarks = Convert.ToInt16(tbtobtmarks.Text);
            decimal p1 = Convert.ToDecimal(tbtobtmarks.Text);
            decimal p2 = Convert.ToDecimal(tbtotmarks.Text);
            decimal p = p1 / p2 * 100;
            if (Convert.ToInt64(p) < 40)
                tbgrade.Text = "Fail";
            if (Convert.ToInt64(p) < 50 && Convert.ToInt64(p) >= 40)
                tbgrade.Text = "C";
            if (Convert.ToInt64(p) < 60 && Convert.ToInt64(p) >= 50)
                tbgrade.Text = "B";
            if (Convert.ToInt64(p) <= 100 && Convert.ToInt64(p) >= 60)
                tbgrade.Text = "A";
            tbper.Text = p.ToString();
            rep.percentage = Convert.ToDecimal(tbper.Text);
            rep.grade = tbgrade.Text;
            rep.remark_id = Convert.ToInt64(ddlremarkid.SelectedValue.ToString());
            rep.isdeleted = false;
            rep.SaveRecords();
            Response.Redirect("~/teachers/TReport.aspx");
        }

        protected void ddlclassid_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = stud.GetRecords(-99, "", -99, Convert.ToInt64(ddlclassid.SelectedValue.ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
            ddlstudentid.DataSource = dt;
            ddlstudentid.DataTextField = "student_name";
            ddlstudentid.DataValueField = "student_id";
            ddlstudentid.DataBind();
        }

        protected void ddlstudentid_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = stud.GetRecords(Convert.ToInt64(ddlstudentid.SelectedValue.ToString()), "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
            tbrollno.Text = dt.Rows[0]["roll_no"].ToString();

        }

        protected void ddlsubjectid_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlremarkid_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            rep.report_id = Convert.ToInt64(tbreportid.Text);
            rep.examination = ddexam.SelectedValue.ToString();
            rep.class_id = Convert.ToInt64(ddlclassid.SelectedValue.ToString());
            rep.student_id = Convert.ToInt64(ddlstudentid.SelectedValue.ToString());
            rep.subject_id = Convert.ToInt64(ddlsubjectid.SelectedValue.ToString());
            rep.max_theorymarks = Convert.ToInt16(tbtheorymax.Text);
            rep.max_practicalmarks = Convert.ToInt16(tbpracmax.Text);
            rep.max_projectmarks = Convert.ToInt16(tbpromax.Text);
            rep.obt_theorymarks = Convert.ToInt16(tbtheoryobt.Text);
            rep.obt_practicalmarks = Convert.ToInt16(tbpracobt.Text);
            rep.obt_projectmarks = Convert.ToInt16(tbproobt.Text);
            int a1 = Convert.ToInt16(tbtheorymax.Text);
            int a2 = Convert.ToInt16(tbpracmax.Text);
            int a3 = Convert.ToInt16(tbpromax.Text);
            int s1 = a1 + a2 + a3;
            tbtotmarks.Text = s1.ToString();
            int b1 = Convert.ToInt16(tbtheoryobt.Text);
            int b2 = Convert.ToInt16(tbpracobt.Text);
            int b3 = Convert.ToInt16(tbproobt.Text);
            int s2 = b1 + b2 + b3;
            tbtobtmarks.Text = s2.ToString();
            rep.total_maxmarks = Convert.ToInt16(tbtotmarks.Text);
            rep.total_obtmarks = Convert.ToInt16(tbtobtmarks.Text);
            decimal p1 = Convert.ToDecimal(tbtobtmarks.Text);
            decimal p2 = Convert.ToDecimal(tbtotmarks.Text);
            decimal p = p1 / p2 * 100;
            if (Convert.ToInt64(p) < 40)
                tbgrade.Text = "Fail";
            if (Convert.ToInt64(p) < 50 && Convert.ToInt64(p) >= 40)
                tbgrade.Text = "C";
            if (Convert.ToInt64(p) < 60 && Convert.ToInt64(p) >= 50)
                tbgrade.Text = "B";
            if (Convert.ToInt64(p) <= 100 && Convert.ToInt64(p) >= 60)
                tbgrade.Text = "A";
            tbper.Text = p.ToString();
            rep.percentage = Convert.ToDecimal(tbper.Text);
            rep.grade = tbgrade.Text;
            rep.remark_id = Convert.ToInt64(ddlremarkid.SelectedValue.ToString());
            rep.isdeleted = false;
            rep.SaveRecords();
            Response.Redirect("~/teachers/TReport.aspx");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            DataTable dt1 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
            DataTable dt = rep.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, Convert.ToInt64(ddsubject.SelectedValue.ToString()), -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            DataTable dt = rep.GetRecords(-99, "", -99, -99, Convert.ToInt64(ddsubject0.SelectedValue.ToString()), -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }

    }
}
